package com.itheima.common.utills;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;

import java.util.HashMap;
import java.util.Map;

@Slf4j
public class AddressUtil {

    @Value("${sky.baidu.ak}")
    private String ak;

    public static String getAddressByIP(String ip){
        // 获取发送请求
        Map map = new HashMap();
        map.put("ak", "ak");
        map.put("ip", ip);
        map.put("coor", "bd09ll ");
        String shopLocation = HttpUtil.get("https://api.map.baidu.com/location/ip", map);
        JSONObject jsonObject = JSON.parseObject(shopLocation);

        if (!jsonObject.getString("status").equals("0")) {
            return "本地机";
        }

        // 根据解析结果，获取经纬度坐标
        JSONObject location = jsonObject.getJSONObject("content").getJSONObject("address_detail");
        //省份
        String province = location.getString("province");
        //城市
        String city = location.getString("city");
        return province + "|" + city;
    }

}
